package com.db.williamchart.slidertooltip;

import com.db.williamchart.Tooltip;
import ohos.agp.colors.RgbColor;
import ohos.agp.components.Component;
import ohos.agp.components.ComponentContainer;
import ohos.agp.components.DirectionalLayout;
import ohos.agp.components.LayoutScatter;
import ohos.agp.components.element.ShapeElement;
import ohos.agp.utils.Color;

public class SliderTooltip implements Tooltip {

    private DirectionalLayout component;

    private int color = Color.WHITE.getValue();

    @Override
    public void onCreateTooltip(ComponentContainer parent) {
        if (component == null){
            component = (DirectionalLayout) LayoutScatter.getInstance(parent.getContext()).parse(ResourceTable.Layout_tooltip_layout,parent,false);
            ShapeElement shapeElement = new ShapeElement();
            shapeElement.setRgbColor(new RgbColor(color));
            component.setBackground(shapeElement);
            component.setVisibility(Component.INVISIBLE);
            parent.addComponent(component);
        }

    }

    @Override
    public void onDataPointTouch(Float x, Float y) {
        if (component != null){
            component.setVisibility(Component.VISIBLE);
            component.setTranslationX((x - component.getWidth() / 2f) - component.getLeft());
        }

    }

    @Override
    public void onDataPointClick(Float x, Float y) {

    }

    /**
     * setting color
     * @param color color value 'rgba'
     */
    public void setColor(int color){
        if (component != null){
            ShapeElement shapeElement = new ShapeElement();
            shapeElement.setRgbColor(new RgbColor(color));
            component.setBackground(shapeElement);
        }
    }
}
